<?php

/*
 * 标题：阿里紫订单管理系统
 * 作者：忆天网络 QQ715457186 
 * 官方网址：www.fjd100.com||www.yi66.net
 */
defined('THINK_PATH') OR exit();
class AliziAction extends Action{
    
        protected $aliziConfig="";//阿里紫配置信息
        protected $aliziHost =""; //阿里紫地址
        public function _init(){ 
			//if(empty($_SESSION['member']) && !in_array(ACTION_NAME,array('login','pass','reg'))){  header('location:'.C('ALIZI_ROOT').'index.php?m=User&a=login&goto='.urlencode("http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"));}


            $this->aliziAuth();
            if(!file_exists('./Public/Database/install.lock')){ header('location:index.php?m=Install');exit;}
            $this->aliziConfig = $this->aliziConfig();
			
            $wap = C('WAP_THEME');
			$this->wap_theme = $wap?$wap:'Item';
			
			$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; 
            $this->host = $http_type.$_SERVER['HTTP_HOST'];
            $this->aliziHost = $this->host.C('ALIZI_ROOT');
            $this->assign('aliziHost',$this->aliziHost);
            $this->assign('aliziConfig',$this->aliziConfig);
            $this->assign('wap_theme',$this->wap_theme);
            $this->ipDenied();
            
            //伪静态后参数
            if(C('URL_MODEL')==2){
                if(isset($_SERVER['HTTP_X_REWRITE_URL'])){
                    $uri = $_SERVER['HTTP_X_REWRITE_URL'];
                }else{
                    $uri = $_SERVER['REQUEST_URI'];
                }
                $url = parse_url($uri);
                if(isset($url['query'])){
                    parse_str($url['query'],$query);
                    $_GET = array_merge($_GET,$query);
                }
            }
			if(isset($_GET['gzid'])){  cookie('ac',$_GET['gzid']); }
            if(isset($_GET['ac'])){  cookie('ac',$_GET['ac']); }
            if(isset($_GET['uid'])){  cookie('uid',$_GET['uid']); }
        }

        public function verify(){
            import('ORG.Util.Image');
            $lenght = isset($_GET['lenght'])?$_GET['lenght']:4;
            $width = isset($_GET['width'])?$_GET['width']:55;
            $height = isset($_GET['height'])?$_GET['height']:32;
            Image::buildImageVerify($lenght,1,'png',$width,$height);
        }

    public function aliziConfig(){
        $config = cache('aliziConfig');
        if(empty($config)){
            $list = M('Setting')->select();
            foreach($list as $li) $config[$li['name']] = $li['value'];
            cache('aliziConfig',$config,8640000);
        }
        return $config;
    }
    public function getAliziPayment($item_id){
        return  R('Api/getAliziPayment',array('item_id'=>$item_id));
    }
    public function getItemParams($opt='',$options=array()){
        $checked = empty($opt)?C('DEFAULT_OPTIONS'):json_decode($opt,true);
        
        $options = $options?$options:C('TEMPLATE_OPTIONS');
        foreach($options as $k=>$v){
            $options[$k]['checked'] = in_array($k,$checked)?true:false;
        }
        return  $options;
    }

    //黑名单IP
    public function ipDenied(){
		if(!empty($this->aliziConfig['safe_ip_denied'])){
			$ip = get_client_ip();
			$this->aliziConfig['safe_ip_denied'] = str_replace('%', '#', $this->aliziConfig['safe_ip_denied']);
			$ipDenied = explode('#', $this->aliziConfig['safe_ip_denied']);
			foreach($ipDenied as $ips){
				if( (strstr($ips, '*') && preg_match('/'.$ips.'/', $ip)) || $ips==$ip){
					 header('HTTP/1.1 404 Not Found'); 
					die('Access Denied');
				}
			}
		}
    }
    function aliziAuth(){
        $cacheName = password('aliziAuth');
        $authCode = cache($cacheName);
        $code = C('ALIZI_AUTH');
        $md5Code = password($code);
		return true;

        header('Content-Type:text/html;charset=utf-8');
        die('<!DOCTYPE html><html><head><meta charset="utf-8"><title>阿里紫订单管理系统</title></head><body>域名：'.$host.' 报错，请联系作者！<br>旺旺：我有一席幽梦<br>店铺：https://yi66net.taobao.com</body></html>');
    }
	
	//短信发送
	function send_sms($mobile,$content){
		$config = $this->aliziConfig();
		$data = array(
			'method'=>'single',
			'account'=>$config['sms_account'],
			'apikey'=>$config['sms_password'],
			'mobile'=>$mobile,
			'content'=>$content,
		);
		$rs = http(C('ALIZI_API').'/sms/send/','POST',$data);
		return json_decode($rs,true);

	}
	function ping($address) {  
		$status = -1;  
		if (strcasecmp(PHP_OS, 'WINNT') === 0) {  
			// Windows 服务器下  
			$pingresult = exec("ping -n 1 {$address}", $outcome, $status);  
		} elseif (strcasecmp(PHP_OS, 'Linux') === 0) {  
			// Linux 服务器下  
			$pingresult = exec("ping -c 1 {$address}", $outcome, $status);  
		}  
		
		if (0 == $status) {  
			preg_match("/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/",implode(" ",$outcome),$ip);
			$status = $ip[0];  
		} else {  
			$status = false;  
		}  
		return $status;  
	}  

    public function _empty($name){
            $this->_404();
    }

    protected function systemStatus($moduleName){
        if(in_array($moduleName, array('Index','Item','Wap'))){

			$wap = $this->wap_theme;
			$get = $_GET;unset($get['_URL_']);
            switch ($this->aliziConfig['system_status']) {
                case '0':
                    R('Order/index',array('id'=>$this->aliziConfig['system_close_info'],'tpl'=>'detail',));exit;
                   // $this->_404($this->aliziConfig['system_close_info'],$this->aliziConfig['system_close_info']);exit;
                    break;
               case '2':  
                if($moduleName=='Index'){
                    //$this->redirect($wap.'/'.ACTION_NAME,$get);exit;
					$url = U($wap.'/'.ACTION_NAME,$get);
					header("location:".$url);exit;
                }
               break;
               case '3':  
                if($moduleName==$wap){
                    $this->redirect('/');exit;
                }
               break;
               default:
                if(isMobile()==true && in_array($moduleName, array('Index'))){
                   $this->redirect($wap.'/'.ACTION_NAME,$get);exit;
                }
                break;
            }
        }
    }
	
	public function _404($title='404',$info='404 Not Found'){
		$this->assign('title',$title);
		$this->assign('info',$info);
		$this->display('Order:404');
	}
}

